package easy;

public class Solution_1071 {

    public String gcdOfStrings(String str1, String str2) {

        if (str1.length() == str2.length()) {
            return str1.equals(str2) ? str1 : "";
        } else if (str1.length() < str2.length()) {
            return gcdOfStrings(str2, str1);
        } else {
            String sub = str1.substring(str2.length());
            int length = sub.length();
            StringBuffer s1 = new StringBuffer();
            StringBuffer s2 = new StringBuffer();
            for (int i = length; i > 0; i--) {
                if (length % i == 0) {
                    String substring = sub.substring(0, i);
                    for (; s1.length() < str1.length(); s1.append(substring)) {
                    }

                    for (; s2.length() < str2.length(); s2.append(substring)) {
                    }
                    if (s1.toString().equals(str1) && s2.toString().equals(str2)) {
                        return substring;
                    }
                    s1.setLength(0);
                    s2.setLength(0);
                }
            }
        }
        return "";
    }

    public static void main(String[] args) {
        Solution_1071 model = new Solution_1071();
        System.out.println(model.gcdOfStrings("ABCABC", "ABC"));
        System.out.println(model.gcdOfStrings("ABABAB", "ABAB"));
        System.out.println(model.gcdOfStrings("LEET", "CODE"));
        System.out.println(model.gcdOfStrings("EFGABC", "ABC"));
        System.out.println(model.gcdOfStrings("TAUXXTAUXXTAUXXTAUXXTAUXX", "TAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXX"));
//        System.out.println("a".substring(1) == "");
    }
}
